import http.client, random


class WoniuSales:
    def __init__(self):
        self.host = 'localhost'
        self.port = 8081

    # 打开首页
    def open_homepage(self):
        conn = http.client.HTTPConnection(self.host, self.port)
        conn.request('GET', '/WoniuSales/')
        # resp = conn.getresponse().read().decode()
        headers = conn.getresponse().getheaders()
        # print(headers)
        # print(resp)

    # 登陆
    def login(self):
        conn = http.client.HTTPConnection(self.host, self.port)
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        data = 'username=admin&password=admin123&verifycode=0000'
        # data = {'username':'admin', 'password':'admin123', 'verifycode':'0000'}
        conn.request('POST', '/WoniuSales/user/login', headers=headers, body=data)
        # resp = conn.getresponse().read().decode()
        self.cookie = conn.getresponse().getheader('Set-Cookie')
        return self.cookie
        # self.cookie += cookie
        # print(resp)
        # print(self.cookie)

    # 新增会员
    def add_customer(self, customname, num, sex, date, creditkids, creditcloth):
        self.login()
        conn = http.client.HTTPConnection(self.host, self.port)
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': self.cookie}
        data = 'customername=%s&customerphone=%s&childsex=%s&childdate=%s&creditkids=%d&creditcloth=%d' \
               % (customname, num, sex, date, creditkids, creditcloth)
        conn.request('POST', '/WoniuSales/customer/add', headers=headers, body=data.encode())
        resp = conn.getresponse().read().decode()
        # print(resp)
        return resp


if __name__ == '__main__':
    ws = WoniuSales()
    # ws.login()
    ws.add_customer(12344321)
